package com.example.fitapp.activity.http.util;

import android.content.SharedPreferences;

import com.example.fitapp.activity.activity.MyApplication;

import java.io.IOException;

import okhttp3.Credentials;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

import static android.content.Context.MODE_PRIVATE;

public class BasicAuthInterceptor implements Interceptor {

    private String credentials;
    public static String getToken() {
        SharedPreferences pref = MyApplication.getContext().getSharedPreferences("data",MODE_PRIVATE);
        String token = pref.getString("token","");
        return token;
    }


    @Override
    public Response intercept(Chain chain) throws IOException {
        credentials = Credentials.basic(getToken(), "");
        Request request = chain.request();
        Request authenticatedRequest = request.newBuilder()
                .header("Authorization", credentials).build();
        return chain.proceed(authenticatedRequest);
    }

}